<template>
  <!-- 资讯列表页面 -->
  <div>
    <!-- 模糊搜索、查询按钮 -->
    <div class="search">
      <div>
        <el-button type="primary" size="mini" class="btn" @click="toPublish"
          >发布风采</el-button
        >
      </div>
      <div>
        <el-form :inline="true" :model="searchForm" class="demo-form-inline">
          <el-form-item>
            <el-select
              clearable
              v-model="searchForm.status"
              placeholder="请选择状态"
              size="mini"
            >
              <el-option label="未审核" value="未审核"></el-option>
              <el-option label="审核不通过" value="审核不通过"></el-option>
              <el-option label="审核通过" value="审核通过"></el-option>
              <el-option label="推荐" value="推荐"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-input
              clearable
              v-model="searchForm.title"
              placeholder="请输入标题"
              size="mini"
            />
          </el-form-item>
          <el-form-item>
            <el-select
              clearable
              v-model="searchForm.categoryId"
              placeholder="请选择栏目"
              size="mini"
            >
              <el-option
                v-for="item in categoryData"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
              <!-- <el-option label="下架" value="下架"></el-option> -->
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click="toSearch" type="primary" size="mini"
              >查询</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
    <!-- 表格 -->
    <div>
      <el-table
        stripe
        size="mini"
        v-loading="loading"
        :data="tableData"
        style="width: 100%"
        @row-click="toDetails"
      >
        <el-table-column type="index" label="序号" width="100" align="center">
        </el-table-column>
        <el-table-column label="封面" width="250" align="center">
          <template slot-scope="scope">
            <el-image
              size="mini"
              style="width: 100%"
              :src="scope.row.cover"
              :preview-src-list="[scope.row.cover]"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column label="风采" width="200" align="center">
          <template slot-scope="scope">
            <div style="font-weight: bold">{{ scope.row.title }}</div>
            <div>{{ scope.row.baseUser && scope.row.baseUser.realname }}</div>
            <div>{{ scope.row.category && scope.row.category.name }}</div>
            {{ moment(scope.row.publishTime).format("YYYY-MM-DD") }}
          </template>
          <!-- <template></template> -->
        </el-table-column>
        <el-table-column label=""></el-table-column>
        <el-table-column
          align="center"
          fixed="right"
          prop="status"
          label="状态"
          width="150"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status === '审核通过'" type="success">{{
              scope.row.status
            }}</el-tag>
            <el-tag
              v-else-if="scope.row.status === '审核不通过'"
              type="danger"
              >{{ scope.row.status }}</el-tag
            >
            <el-tag v-else-if="scope.row.status === '未审核'" type="info">{{
              scope.row.status
            }}</el-tag>
            <el-tag v-else-if="scope.row.status === '推荐'" type="warning">{{
              scope.row.status
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" fixed="right" label="操作" width="200">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              :disabled="scope.row.status === '推荐'"
              icon="el-icon-star-off"
              @click.stop="recommend(scope.row.id, '推荐')"
              >推荐</el-button
            >
            <el-button
              type="text"
              size="small"
              class="green_info"
              icon="el-icon-warning-outline"
              @click.stop="check(scope.row)"
              >审核</el-button
            >
            <el-button
              type="text"
              size="small"
              class="red_info"
              icon="el-icon-delete"
              @click.stop="toDelete(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="pagination-div">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.page"
        :page-sizes="[5, 10, 15, 20, 30]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
    <!-- 模态框 -->
    <el-dialog title="审核风采信息" :visible.sync="visible" width="60%">
      <div>
        <div style="text-align: center; font-weight: bold">
          {{ article.title }}
        </div>
        <div v-html="article.content" />
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="danger" @click="audit(article.id, '审核不通过')"
          >审核不通过</el-button
        >
        <el-button type="primary" @click="audit(article.id, '审核通过')"
          >审核通过</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { pageQuery, deleteById, changeStatus, findById } from "@/api/article";
import { findAll } from "@/api/infoCategory";
import moment from "moment";
import Tinymce from "@/components/Tinymce";

export default {
  components: {
    Tinymce,
  },
  data() {
    return {
      tableData: [],
      // 产品栏目的数据，页面一上来，就发请求，请求所有的产品栏目数据，设置到categoryData。
      categoryData: [],
      // 模糊搜索表单的数据
      searchForm: {
        title: "",
        status: "",
        categoryId: "",
      },
      // 分页参数
      params: {
        page: 1,
        pageSize: 10,
      },
      // 总条数
      total: 0,
      // 加载
      loading: false,
      // 模态框的显示与隐藏
      visible: false,
      article: {},
    };
  },
  watch: {
    // 深度监听分页参数
    params: {
      handler() {
        this.queryData();
      },
      deep: true,
    },
  },
  computed: {},
  methods: {
    moment,
    // 获取栏目分类数据
    async queryCategoryData() {
      let temp = {};

      // 遍历对象
      for (let key in temp) {
        if (!temp[key]) {
          delete temp[key];
        }
      }
      let res = await findAll(temp);
      // console.log(res.data.list);
      this.categoryData = res.data;
    },
    // 分页模糊获取产品数据
    async queryData() {
      this.loading = true;
      let temp = {
        ...this.params,
        ...this.searchForm,
      };
      // 遍历对象
      for (let key in temp) {
        if (!temp[key]) {
          delete temp[key];
        }
      }
      let res = await pageQuery(temp);
      // 加载完毕
      this.loading = false;
      this.tableData = res.data.list;
      this.total = res.data.total;
    },
    // 查询按钮点击事件处理程序
    toSearch() {
      this.queryData();
    },
    // 每页显示的条数发生变化时的事件处理程序
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      // this.params.pageSize = val;
      // this.params.page = 1;
      this.params = {
        page: 1,
        pageSize: val,
      };
      //this.queryData()
    },
    // 当前页码发生变化时的事件处理程序
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.params.page = val;
      //this.queryData()
    },
    // 删除按钮
    toDelete(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await deleteById({ id });
          this.$notify.success({
            title: "成功",
            message: res.success,
          });
          this.queryData();
        })
        .catch(() => {
          this.$notify.info({
            title: "取消",
            message: "已取消删除",
          });
        });
    },
    // 推荐按钮
    async recommend(id, status) {
      this.$confirm("您确定要推荐此资讯吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const data = { id, status };
          const res = await changeStatus(data);
          this.$notify.success({
            title: "成功",
            message: res.success,
          });
          this.queryData();
        })
        .catch(() => {
          this.$notify.info({
            title: "取消",
            message: "已取消推荐",
          });
        });
    },
    // 审核按钮
    check(row) {
      this.visible = true;
      this.article = row;
    },
    // 审核
    async audit(id, status) {
      const data = { id, status };
      const res = await changeStatus(data);
      this.$notify.success({
        title: "成功",
        message: res.success,
      });
      this.visible = false;
      this.queryData();
    },
    // 跳转到发布页面
    toPublish() {
      this.$router.push({
        path: "/admin/info/article/Publish",
      });
    },
    // 通过id查找风采
    async findArticleById(id) {
      const res = await findById(id);
    },
    // 风采详情
    toDetails(row) {
      this.$router.push({
        path: "/admin/info/article/Details",
        query: { id: row.id },
      });
    },
  },
  created() {
    this.queryCategoryData();
    this.queryData();
  },
  mounted() {},
};
</script>
<style lang="scss" scoped>
.search {
  display: flex;
  // height: 40px;
  .btn {
    margin-right: 10px;
    margin-top: 5px;
  }
}
</style>